def find_quotes_com(com):
    res = ""
    for i in range(com.index("\"")+1, com.rindex("\"")):
        res += com[i]
    return res

def find_dict(res, input_str, i=0):
    tmp_dict = {}
    for j in range(i, len(input_str)):
        if "{" in input_str[j]:
            return tran_to_dict(res, input_str, j, tmp_dict)

def tran_to_dict(res, input_str, i, tmp_dict):
    for j in range(i+1, len(input_str)):
        if "}" in input_str[j]:
            res.append(tmp_dict)
            return find_dict(res, input_str, j)
        tmp = input_str[j].split(":")
        tmp_dict[find_quotes_com(tmp[0])] = find_quotes_com(tmp[1])


class Handler:
    """处理值"""

    def __init__(self, value):
        self.value = value

    def handle_array(self, array, next=None):
        res = []
        while next != "done":
            if next is not None:
                array = next
            value = ""
            num = 0
            for i in range(len(array)):
                value += array[i]
                if array[i] == "(":
                    num += 1
                if array[i] == ")":
                    num -= 1
                if i == len(array)-1:
                    res.append(value)
                    next = "done"
                else:
                    if array[i+1] == "," and num == 0:
                        res.append(value)
                        next = array[i+2: ]
                        break
        res = [self.parse(i) for i in res]
        return res

    def handle_row(self, row, next=None):
        res = {}
        while next != "done":
            if next is not None:
                row = next
            tmp = row.index(" ")
            key = row[: tmp]
            value = ""
            num = 0
            for i in range(tmp+1, len(row)):
                if row[i] == "(":
                    num += 1
                if row[i] == ")":
                    num -= 1
                value += row[i]
                if i == len(row)-1:
                    res[key] = value
                    next = "done"
                else:
                    if row[i+1] == "," and num == 0:
                        res[key] = value
                        next = row[i+2: ]
                        break
        for i in res.items():
            res[i[0]] = self.parse(i[1])
        return res

    def parse(self, value=None):
        if value is None:
            value = self.value
        if "(" in value:
            flag = value[: value.index("(")]  # 取括号前的字符
            tmp = ""
            for i in range(value.index("(")+1, value.rindex(")")):  # 取括号中的内容
                tmp += value[i]
            if flag == "array":
                return self.handle_array(tmp)
            elif flag == "row":
                return self.handle_row(tmp)
            else:
                raise ValueError("flag is not array or row")
        else:
            return value


def step1(input_str):
    res = []
    find_dict(res, input_str)
    return res

def step2(p):
    for i in range(len(p)):
        for j in p[i].items():
            handler = Handler(j[1])
            p[i][j[0]] = handler.parse()
    return p


with open(r"D:\projects\parse-new-json\cmu1h_report") as f:
    input_str = f.readlines()
    p = step1(input_str)  # 将文本转换为 python 基础数据类型
    result = step2(p)  # 解析 array row varchar

